package com.yanqu.road.server.logic.activity.tradewar.condition;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tradewar.TradeWarActivityModule;
import com.yanqu.road.server.logic.activity.tradewar.TradeWarActivityData;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.activity.TradeWarActivityRankMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

public abstract class BaseTradeWarActivityUnionRankCondition extends BaseTradeWarActivityCondition{

    public BaseTradeWarActivityUnionRankCondition(GamePlayer player, TradeWarActivityData activityData, ActivityConditionInfo conditionInfo) {
        super(player, activityData, conditionInfo);
    }

    public final void setUnionRankActivityDefaultValue(String unionUid, int notifyType) {
        if(!StringUtils.isNullOrEmpty(unionUid) && TradeWarActivityMgr.unionCanCrossTradeWar(unionUid)) {
            UserActivityConditionData userActivityConditionData = activityData.getUserActivityConditionData(getConditionId());
            setUnionActivityData(unionUid, notifyType, userActivityConditionData);
        }
    }

    public final void setUnionRankActivityValue(BigInteger value, String unionUid, int notifyType) {
        UserActivityConditionData userActivityConditionData = activityData.getUserActivityConditionData(getConditionId());
        userActivityConditionData.setValue(value);
        setUnionActivityData(unionUid, notifyType, userActivityConditionData);
    }

    private void setUnionActivityData(String unionUid, int notifyType, UserActivityConditionData userActivityConditionData) {
        if (UnionRiseRankArgs.NOTIFY_TYPE_NORMAL == notifyType || UnionRiseRankArgs.NOTIFY_TYPE_JOIN == notifyType) {
            userActivityConditionData.setUnionUid(player.getUserInfo().getUnionUid());
        } else if (UnionRiseRankArgs.NOTIFY_TYPE_REMOVE == notifyType || UnionRiseRankArgs.NOTIFY_TYPE_DELETE == notifyType) {
            userActivityConditionData.setUnionUid("");
        }
        TradeWarActivityRankMgr.changeUnionActivityValue(activityData.getActivityId(), unionUid, player.getUserId(), userActivityConditionData, notifyType);
        player.getModule(TradeWarActivityModule.class).onActivityDataChange(activityData);
    }
}
